package com.tcm.controller.front;

import com.tcm.common.enums.ResponseEnum;
import com.tcm.entity.dto.param.NoteUpdateDto;
import com.tcm.entity.resp.R;
import com.tcm.service.NoteService;
import com.tcm.utils.AccessTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 学生笔记控制器
 * @author Guqier
 * @version 1.0
 * @date 2023/8/22 16:13
 */
@RestController
@RequestMapping("/stu/note")
public class StuNoteController {

    @Autowired
    private NoteService noteService;

    /**
     * 学生保存笔记
     * @param dto 笔记更新参数
     * @param request 请求对象
     * @return 响应结果
     */
    @RequestMapping(method = RequestMethod.POST)
    public R saveNote(@RequestBody @Valid NoteUpdateDto dto, HttpServletRequest request){
        return noteService.saveNote(dto, AccessTokenUtil.getUserId(request)) ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

    /**
     * 删除笔记
     * @param itemId 题目ID
     * @return 删除结果
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public R removeNote(@RequestParam Long itemId, HttpServletRequest request){
        return noteService.removeNote(itemId, AccessTokenUtil.getUserId(request)) ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

    /**
     * 根据题目ID获取笔记
     * @param itemId 题目ID
     * @param request 请求对象
     * @return 笔记信息
     */
    @RequestMapping(value = "/by/item", method = RequestMethod.GET)
    public R getNoteByItemId(@RequestParam Long itemId, HttpServletRequest request){
        NoteUpdateDto noteUpdateDto = noteService.getNoteByItemId(itemId, AccessTokenUtil.getUserId(request));
        if (noteUpdateDto == null){
            return R.fail(ResponseEnum.NOTE_NOT_EXIST);
        }
        return R.success(noteUpdateDto);
    }

    /**
     * 学生获取笔记列表
     * @param request 请求对象
     * @return 响应结果
     */
    @RequestMapping(method = RequestMethod.GET)
    public R getStuNoteList(HttpServletRequest request){
        return R.success(noteService.getStuNoteList(AccessTokenUtil.getUserId(request)));
    }

    /**
     * 获取笔记详细
     * @param current 当前笔记序数
     * @param request 请求对象
     * @return 笔记详细
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public R getNoteDetail(@RequestParam Integer current, HttpServletRequest request){
        return R.success(noteService.getNoteDetail(current, AccessTokenUtil.getUserId(request)));
    }

}
